﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class FrmAuthor : DevComponents.DotNetBar.OfficeForm
    {
        private bool addNew = true;
        public bool viewDetail = false;
        Author author = null;
        AuthorManager authorManager;
        public FrmAuthor()
        {
            InitializeComponent();
        }
        public FrmAuthor(string authorID, bool detail)
        {
            InitializeComponent();
            this.Text = "Cập nhật thông tin tác giả: " + authorID;
            txtAuthorID.Enabled = false;
            addNew = false;
            authorManager = new AuthorManager();
            if (detail)
            {
                viewDetail = true;
                btnReset.Visible = false;
                this.Text = "Xem thông tin tác giả: " + authorID;
                btnSubmit.Text = "Đóng cửa sổ";
                btnSubmit.Location = new Point((int)this.Width / 2 - 50, btnSubmit.Location.Y);
            }
            author = authorManager.GetDetail(authorID);
            txtAuthorID.Text = author.AuthorID;
            txtAuthorName.Text = author.AuthorName.ToString();
            txtAuthorBirth.Text = author.Birth.ToString();
            txtAuthorDead.Text = author.Dead.ToString();
            txtIntroduction.Text = author.Description.ToString();
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string result = "";
            //Parse to Author
            int dead = DataUtil.ToInt(txtAuthorDead.Text, 0);
            int birth = DataUtil.ToInt(txtAuthorBirth.Text, 0);
            author = new Author();
            author.AuthorID = txtAuthorID.Text;
            author.AuthorName = txtAuthorName.Text;
            author.Description = txtIntroduction.Text;
            if (dead == 0)
                author.Dead = null;
            else
                author.Dead = dead;
            if (birth == 0)
                author.Birth = null;
            else
                author.Birth = birth;
            //End parse to author
            author.Status = "Khả dụng";
            try
            {
                authorManager = new AuthorManager();
                if (addNew)
                {
                    result = authorManager.Insert(author);
                }
                else
                {
                    result = authorManager.Update(author);
                }
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtAuthorID.ResetText();
            txtAuthorName.ResetText();
            txtAuthorID.ResetText();
            txtAuthorDead.ResetText();
            txtAuthorBirth.ResetText();
            txtIntroduction.ResetText();
        }
    }
}